home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: System Software / Apple_Developer_Group_April_1996_System_Software.iso / What's New? / Sample Code / Snippets Update 1 / PopUpMenuSelectWithCurFont / PopUpMenuSelectWithCurFont.c next >
Encoding:
Text File  |  1996-02-01  |  4.5 KB  |  197 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    PopUpMenuSelectWithCurFont demonstrates which low memory globals
  3.     //    (and possibly even nastier things) one must twiddle in order to
  4.     //    control the font used by MDEF 0 during PopUpMenuSelect. Note that
  5.     //    aside from the compatibility code associated with HOSTED_BY_FONT_MISCREANT,
  6.     //    this code roughly parallels what the popup menu CDEF (ID 63) does.
  7.     //    If at all possible you should make use of the CDEF, because when
  8.     //    Engineering breaks the low memory trick, we can fix the CDEF, but
  9.     //    we can't fix your code.
  10.     //
  11.     //    Complaints and kudos to:
  12.     //
  13.     //        Pete Gontier
  14.     //        Apple Macintosh Developer Technical Support
  15.     //        <gurgle@apple.com>
  16.     //
  17.  
  18. #define OLDROUTINELOCATIONS        0
  19. #define OLDROUTINENAMES            0
  20. #define SystemSevenOrLater        1
  21.  
  22. #ifndef __FONTS__
  23. #    include <Fonts.h>
  24. #endif
  25.  
  26. #ifndef __DIALOGS__
  27. #    include <Dialogs.h>
  28. #endif
  29.  
  30. #ifndef __RESOURCES__
  31. #    include <Resources.h>
  32. #endif
  33.  
  34. #ifndef __LOWMEM__
  35. #    include <LowMem.h>
  36. #endif
  37.  
  38.     //
  39.     //    HOSTED_BY_FONT_MISCREANT
  40.     //
  41.     //    If you are writing an external code resource (filter,
  42.     //    XCMD, etc.), and this code isn't giving you any joy,
  43.     //    let HOSTED_BY_FONT_MISCREANT be 1 and see if that helps.
  44.     //    Some extension hosts set up a hostile font environment.
  45.     //    Your lack of joy is their fault, and they force you
  46.     //    to assume even more compatibility risk than you would
  47.     //    by using this code without HOSTED_BY_FONT_MISCREANT
  48.     //    defined. On the other hand, we *are* weaving a tangled
  49.     //    web by twiddling low memory, and sometimes one must pay
  50.     //    the price for this sort of behavior.
  51.     //
  52.  
  53. #define HOSTED_BY_FONT_MISCREANT 0
  54.  
  55. #if HOSTED_BY_FONT_MISCREANT
  56.  
  57. static pascal GrafPtr GetSomeWindowManagerPort (void)
  58. {
  59.     //
  60.     //    Produces a pointer to the Window Manager port or
  61.     //    Color Window Manager Port. You can test the returned
  62.     //    GrafPtr to see whether it's colored or not, but
  63.     //    many callers won't care.
  64.     //
  65.  
  66.     GrafPtr result = nil;
  67.  
  68.     SysEnvRec theWorld;
  69.  
  70.     //
  71.     //    Yes, SysEnvirons is deprecated. But it still works,
  72.     //    and it's a cheap way to buy backward compatibility
  73.     //    if you only need to know whether CQD support exists.
  74.     //
  75.  
  76.     if (SysEnvirons (1, &theWorld))
  77.         DebugStr ("\pWhoa! Panic! SysEnvirons failed?!");
  78.     else if (theWorld.hasColorQD)
  79.         GetCWMgrPort ((CGrafPtr *) &result);
  80.     else
  81.         GetWMgrPort (&result);
  82.  
  83.     return result;
  84. }
  85.  
  86. #endif
  87.  
  88. static pascal void PopUpMenuSelectWithCurFont
  89.     (MenuRef popUpMenuRef, Point where, unsigned short prevSelection)
  90. {
  91.     GrafPtr hostPort;
  92.  
  93.     short oldSysFont = LMGetSysFontFam ( );
  94.     short oldSysSize = LMGetSysFontSize ( );
  95.  
  96.     GetPort (&hostPort);
  97.  
  98.     LMSetSysFontFam (hostPort->txFont);
  99.     LMSetSysFontSize (hostPort->txSize);
  100.     LMSetLastSPExtra (-1);
  101.  
  102. #if HOSTED_BY_FONT_MISCREANT
  103.     SetPort (GetSomeWindowManagerPort ( ));
  104.     TextFont (0);
  105.     TextSize (0);
  106.     SetPort (hostPort);
  107. #endif
  108.  
  109.     InsertMenu (popUpMenuRef,hierMenu);
  110.     PopUpMenuSelect (popUpMenuRef, where.v, where.h, prevSelection);
  111.     DeleteMenu (128);
  112.  
  113.     LMSetSysFontFam (oldSysFont);
  114.     LMSetSysFontSize (oldSysSize);
  115.     LMSetLastSPExtra (-1);
  116. }
  117.  
  118. //////////////////////////////////////////////////////////////////////
  119. //
  120. //    Below please find the usual sort of application boilerplate.
  121. //
  122. //////////////////////////////////////////////////////////////////////
  123.  
  124. static pascal OSErr InitMac (void)
  125. {
  126.     MaxApplZone ( );
  127.     InitGraf (&(qd.thePort));
  128.     InitFonts ( );
  129.     InitWindows ( );
  130.     InitMenus ( );
  131.     TEInit ( );
  132.     InitDialogs (nil);
  133.  
  134.     return noErr;
  135. }
  136.  
  137. static pascal Boolean ModalFilterProc (DialogPtr theDialog, EventRecord *theEvent, short *)
  138. {
  139.     Boolean result = false;
  140.  
  141.     if (theEvent->what == mouseDown)
  142.     {
  143.         Point localWhere = theEvent->where;
  144.         GrafPtr savePort;
  145.     
  146.         GetPort (&savePort);
  147.         SetPort (theDialog);
  148.         GlobalToLocal (&localWhere);
  149.     
  150.         if (FindDialogItem (theDialog, localWhere) == 1)
  151.         {
  152.             MenuRef popUpMenuRef = GetMenu (128);
  153.             if (popUpMenuRef)
  154.             {
  155.                 short txFont = theDialog->txFont;
  156.                 short txSize = theDialog->txSize;
  157.                 short iType; Handle iHandle; Rect iRect;
  158.                 Point popWhere;
  159.  
  160.                 GetDialogItem (theDialog,2,&iType,&iHandle,&iRect);
  161.  
  162.                 popWhere.v = iRect.bottom;
  163.                 popWhere.h = iRect.left + 2;
  164.                 LocalToGlobal (&popWhere);
  165.  
  166.                 InvertRect (&iRect);
  167.                     TextFont (geneva);
  168.                         TextSize (9);
  169.                             PopUpMenuSelectWithCurFont (popUpMenuRef,popWhere,0);
  170.                         TextSize (txSize);
  171.                     TextFont (txFont);
  172.                 InvertRect (&iRect);
  173.  
  174.                 ReleaseResource ((Handle) popUpMenuRef);
  175.             }
  176.         }
  177.     
  178.         SetPort (savePort);
  179.     }
  180.  
  181.     return result;
  182. }
  183.  
  184. void main (void)
  185. {
  186.     if (!InitMac ( ))
  187.     {
  188.         DialogRef dlgRef = GetNewDialog (128,nil,nil);
  189.         if (dlgRef)
  190.         {
  191.             short itemHit;
  192.             ModalDialog (ModalFilterProc,&itemHit);
  193.             DisposeDialog (dlgRef);
  194.         }
  195.     }
  196. }
  197.